package com.muqi.app.qlearn.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.modles.Body;
import com.muqi.app.qlearn.student.BaseFragment;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.widget.DashboardView;
import com.muqi.app.user.widget.HighlightCR;
import com.muqi.app.user.widget.DashboardView.StripeMode;

@SuppressLint("InflateParams")
public class BannerBodyFragment extends BaseFragment {

	public static final String BANNER_BODY_CHANGED = "body_changed";

	private DashboardView dvH;
	private DashboardView dvW;
	private TextView mtvH;
	private TextView mtvW;
	private BannerChanged bannerChanged;

	public BannerBodyFragment() {
		super();
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		bannerChanged = new BannerChanged();
		IntentFilter filter = new IntentFilter();
		filter.addAction(BANNER_BODY_CHANGED);
		filter.addAction(MContants.CHANGE_CURRENT_CHILD_ACTION);
		getActivity().registerReceiver(bannerChanged, filter);
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_banner_health, null);
		initView(view);
		return view;
	}

	private void initView(View view) {

		loadData();
		dvH = (DashboardView) view.findViewById(R.id.dashboard_height);
		dvW = (DashboardView) view.findViewById(R.id.dashboard_weight);
		dvH.setStripeMode(StripeMode.NORMAL);
		dvW.setStripeMode(StripeMode.NORMAL);
		mtvH = (TextView) view.findViewById(R.id.tv_height);
		mtvW = (TextView) view.findViewById(R.id.tv_weight);
		initBody();

	}

	private void initBody() {
		List<HighlightCR> highlight3 = new ArrayList<HighlightCR>();
		highlight3.add(new HighlightCR(150, 30, Color.parseColor("#242578")));
		highlight3.add(new HighlightCR(180, 40, Color.parseColor("#4090CE")));
		highlight3.add(new HighlightCR(220, 100, Color.parseColor("#17ACED")));
		highlight3.add(new HighlightCR(320, 40, Color.parseColor("#4090CE")));
		highlight3.add(new HighlightCR(0, 30, Color.parseColor("#242578")));
		dvH.setStripeHighlightColorAndRange(highlight3);
		dvW.setStripeHighlightColorAndRange(highlight3);

	}

	/**
	 * 加载身高和体重
	 */
	private void loadData() {
		if (NetWorkUtils.isNetworkAvailable(mActivity)) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("token", mSpUtil.getToken());
			map.put("user_id", mSpUtil.getUserId());
			String requestApi = ParamsUtils.buildParams(
					NetWorkApi.GET_BODY_API, map);
			MyAsyncHttp.get(mActivity, requestApi, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {

					if (resuleCode == RESULTCODE.SUCCESS) {
						Body body = ResponseUtils.getBody(mActivity, result);
						showBody(body);

					}
				}
			});
		} else {
			ShowToast.showShort(mActivity, R.string.net_break);
		}

	}

	/*
	 * 展示当前的身高和体重
	 */
	protected void showBody(Body body) {
		if (body != null) {
			if (body.current_detail_userinfo != null) {
				dvH.setRealTimeValue(Float
						.parseFloat(body.current_detail_userinfo.height)+40);
				dvW.setRealTimeValue(Float
						.parseFloat(body.current_detail_userinfo.weight));
				mtvH.setText(body.current_detail_userinfo.height);

				mtvW.setText(body.current_detail_userinfo.weight);

			}

		}

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().unregisterReceiver(bannerChanged);
	}

	private class BannerChanged extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			loadData();

		}

	}
}
